import java.io.InputStream;
import java.util.Properties;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

import Encheres.Model.Enchere;
import Encheres.Model.EnchereHelper;
import Encheres.service.EnchereService;
import Encheres.service.EnchereServiceHelper;


public class EnchereServeur {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		try {
			new EnchereServeur();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
	public EnchereServeur()
	{
		try
		{
//			InputStream fis = getClass().getResourceAsStream("/orbProperties.properties");
//			Properties prop = new Properties();
//			prop.load(fis);
			
			InputStream is = getClass().getResourceAsStream("/orbProperties.properties");
            Properties props = new Properties();
            props.load(is);

            
//          String [] myArgs = new String [] {"-ORBInitRef NameService=corbaloc::localhost:1050/NameService"};
            
            ORB orb = ORB.init((String[])null, props);
			
			
			//ORB orb = ORB.init(myArgs, null);
			POA rootPoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
			rootPoa.the_POAManager().activate();
			EnchereServiceImpl server = new EnchereServiceImpl();
			server.setORB(orb);
			
			org.omg.CORBA.Object ref = rootPoa.servant_to_reference(server); 
			
			EnchereService href = EnchereServiceHelper.narrow(ref); 
	
			org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); 
	
			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); 
	
			String name = "EnchereServer"; 
			NameComponent path[] = ncRef.to_name( name ); 
			ncRef.rebind(path, href); 
	
			System.out.println("EnchereServer ready and waiting ...");
			orb.run();
		
		} catch(Exception e) {
			System.out.println("ERROR : " + e.getMessage());
			e.printStackTrace();
		}
	}
}
